package com.example.test;

public class Student implements Comparable<Student> {

    private String name;

    private int age;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        final StringBuffer sb = new StringBuffer("Student{");
        sb.append("name='").append(name).append('\'');
        sb.append(", age=").append(age);
        sb.append('}');
        return sb.toString();
    }

    /**
     * 按姓名升序排序
     * 姓名相同，则按年龄升序
     *
     * @param o
     * @return
     */
    @Override
    public int compareTo(Student o) {
        int flag = this.name.compareTo(o.getName());
        if (flag == 0) {
            flag = this.age - o.getAge();
        }
        return flag;
    }
}
